#include "register.h"
#include "ui_register.h"
#include <QMessageBox>
#include <QSqlQuery>
#include <QFile>
#include <QDebug>
#include <QStringList>
#include <QString>
#include "browserview.h"
#include "ui_browserview.h"

Register::Register(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Register)
{
    ui->setupUi(this);
    setWindowTitle("用户注册");
    this->setWindowIcon(QIcon(":/resource/imgs/icon.jpg"));
    setFixedSize(486,305);
    connect(ui->btn,&QPushButton::clicked,this,&Register::on_register2_clicked);
}

void Register::on_register2_clicked()
{
    QString acc = ui->account->text();
    QString username = ui->username->text();
    QString psw = ui->psw->text();

    if(acc == "" ||psw == ""||username == "")
    {
        QMessageBox::information(this,"警告","输入不能为空",QMessageBox::Ok);
    }
    else
    {
        QSqlQuery query;
            query.prepare("select account from user where account=:acc");
            query.bindValue(":acc", acc);
            query.exec();
            if(query.next())
            {
                QMessageBox::information(this,"警告","用户已被注册！",QMessageBox::Ok);
            }
            else
            {
                QSqlQuery q;
                q.prepare("insert into user(account,username,psw)"
                              "values(:acc,:username,:psw)");
                q.bindValue(":acc", acc);
                q.bindValue(":username",username);
                q.bindValue(":psw",psw);

                q.exec();
                QMessageBox::information(this,"警告","注册成功！",QMessageBox::Ok);
                this->close();
            }
    }
}

Register::~Register()
{
    delete ui;
}
